local hx__zishu = fk.CreateSkill {
  name = "hx__zishu",
  tags = { Skill.Compulsory, },
}

hx__zishu:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__zishu.name) then
      if event == fk.AfterCardsMove then
        if player ~= player.room.current then
          for _, move in ipairs(data) do
            if move.to == player and move.toArea == Player.Hand then
              return true
            end
          end
        elseif player.room.current == player then
          for _, move in ipairs(data) do
            if move.to == player and move.toArea == Player.Hand and move.skillName ~= hx__zishu.name then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    if event == fk.AfterCardsMove then
      if player ~= player.room.current then
        for _, move in ipairs(data) do
        if move.to == player and move.toArea == Player.Hand then
          for _, info in ipairs(move.moveInfo) do
            local id = info.cardId
            if room:getCardArea(id) == Card.PlayerHand and room:getCardOwner(id) == player then
              table.insert(cards, id)
            end
          end
        end
      end
      if #cards > 0 then
        player:addToPile("hx__zishu_pile", cards, false, hx__zishu.name)
      end
      elseif player.room.current == player then
        player:drawCards(1, hx__zishu.name)
      end
    end
  end,
})
hx__zishu:addEffect(fk.EventPhaseProceeding, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__zishu.name) then
      if player.phase == Player.Start then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.phase == Player.Start then
      if #player:getPile("hx__zishu_pile") > 0 then
        local cards = player:getPile("hx__zishu_pile")
        room:moveCards({
          ids = cards,
          from = player,
          to = player,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonPut,
          proposer = player,
          skillName = hx__zishu.name,
        })
      end
    end
  end,
})

return hx__zishu